package ai.people.netmon.telegram.service.impl;

import ai.people.netmon.framework.businessenum.AnalysisSourceEnum;
import ai.people.netmon.framework.businessenum.AnalysisTypeEnum;
import ai.people.netmon.framework.domain.searchmsg.request.MessageRequest;
import ai.people.netmon.framework.domain.telegram.entity.AnalysisDataUser;
import ai.people.netmon.framework.domain.telegram.vo.account.AnalyzedAccount;
import ai.people.netmon.framework.domain.telegram.vo.account.ChannelsMember;
import ai.people.netmon.framework.domain.telegram.vo.account.SimilarAccount;
import ai.people.netmon.framework.domain.telegram.vo.channels.ChatMessage;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.telegram.domain.request.AnalyzedListRequest;
import ai.people.netmon.telegram.domain.request.account.ChannelsMemberRequest;
import ai.people.netmon.telegram.domain.request.account.SimilarAccountRequest;
import ai.people.netmon.telegram.domain.request.account.SpeakMessageRequest;
import ai.people.netmon.telegram.mapper.AccountAnalysisMapper;
import ai.people.netmon.telegram.service.AccountAnalysisService;
import ai.people.netmon.telegram.service.AnalysisDataUserService;
import ai.people.netmon.telegram.service.ChannelsAnalysisService;
import ai.people.netmon.telegram.service.MessagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * 账号分析
 *
 * @author yuanqinglong
 * @date 2022/1/12 18:23
 */
@Service
public class AccountAnalysisServiceImpl implements AccountAnalysisService {

    @Autowired
    private AccountAnalysisMapper accountAnalysisMapper;

    @Autowired
    private ChannelsAnalysisService channelsAnalysisServiceImpl;

    @Autowired
    private MessagesService messagesServiceImpl;

    @Autowired
    private AnalysisDataUserService analysisDataUserServiceImpl;
    /**
     * 群组列表
     *
     * @param channelsMemberRequest 群组成员请求参数
     * @return {@link PageResponse}<{@link ChannelsMember}>
     */
    @Override
    public PageResponse<ChannelsMember> channelsList(ChannelsMemberRequest channelsMemberRequest) {
        long channelsCount = accountAnalysisMapper.channelsCount(channelsMemberRequest);
        if (channelsCount == 0) {
            return PageResponse.builder(Collections.emptyList(), channelsCount);
        }
        List<ChannelsMember> channelsMembers = accountAnalysisMapper.channelsList(channelsMemberRequest);
        // TODO: 2022/1/13 判断加入分析标识
        AnalyzedListRequest analyzedListRequest = new AnalyzedListRequest(null, AnalysisTypeEnum.CHANNELS.getCode(), channelsMemberRequest.getAnalysisSource());
        Set<String> analyzedChannelsIdSet = channelsAnalysisServiceImpl.analyzedChannelsIdSet(analyzedListRequest);
        channelsMembers.forEach(channelsMember -> channelsMember.setJoinStatus(analyzedChannelsIdSet.contains(channelsMember.getChannelId())));
        return PageResponse.builder(channelsMembers, channelsCount);
    }

    /**
     * 发言消息
     *
     * @param speakMessageRequest 发言消息请求参数
     * @return {@link PageResponse}<{@link ChatMessage}>
     */
    @Override
    public PageResponse<ChatMessage> speakMessageList(SpeakMessageRequest speakMessageRequest) {
        MessageRequest messageQuery = speakMessageRequest.createMessageQuery( AnalysisSourceEnum.telegram.name());
        return messagesServiceImpl.globalInfoSearch(messageQuery);
    }

    /**
     * 类似账号
     *
     * @param similarAccountRequest 同类账号参数
     * @return {@link List}<{@link SimilarAccount}>
     */
    @Override
    public List<SimilarAccount> similarAccount(SimilarAccountRequest similarAccountRequest) {
        return accountAnalysisMapper.similarAccount(similarAccountRequest);
    }

    /**
     * 已加账户分析菜单
     *
     * @param analyzedListRequest 参数
     * @return {@link List}<{@link AnalyzedAccount}>
     */
    @Override
    public List<AnalyzedAccount> analyzedAccount(AnalyzedListRequest analyzedListRequest) {
        return accountAnalysisMapper.analyzedAccounts(analyzedListRequest);
    }

    /**
     * 已加入分析账户id集合
     *
     * @param analyzedListRequest 请求参数
     * @return {@link Set}<{@link String}>
     */
    @Override
    public Set<String> analyzedAccountIdSet(AnalyzedListRequest analyzedListRequest) {
        return accountAnalysisMapper.analyzedAccountIdSet(analyzedListRequest);
    }

    /**
     * 加入分析
     *
     * @param analysisDataUser 分析数据用户
     * @return int
     */
    @Override
    public int joinAnalysis(AnalysisDataUser analysisDataUser) {
        return analysisDataUserServiceImpl.joinAnalysis(analysisDataUser);
    }


}
